package com.lzy.cloud.producer.config;

import com.lzy.framework.core.cache.cluster.RedisClusterProperties;
import com.lzy.framework.core.datasource.DatasourceProperties;
import com.lzy.framework.core.utils.cryptors.AESCryptor;
import com.lzy.framework.druid.pool.DruidDataSource;
import com.lzy.framework.druid.support.http.StatViewServlet;
import com.lzy.framework.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description: lzy-cloud-service
 *
 * @author: ellengou
 * @date: 2018-05-22 星期二
 * @time: 12:37
 */

@Configuration
@EnableConfigurationProperties(DatasourceProperties.class)
public class DataSourceConfig {
        private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);

        @Autowired
        DatasourceProperties datasourceProperties;

        @Bean
        public ServletRegistrationBean druidServlet() {
            ServletRegistrationBean reg = new ServletRegistrationBean();
            reg.setServlet(new StatViewServlet());
            reg.addUrlMappings("/druid/*");
            reg.addInitParameter("loginUsername", AESCryptor.getInstance().decrypt(datasourceProperties.getUsername()));
            reg.addInitParameter("loginPassword", AESCryptor.getInstance().decrypt(datasourceProperties.getPassword()));
            return reg;
        }

        @Bean
        public FilterRegistrationBean filterRegistrationBean() {
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(new WebStatFilter());
            filterRegistrationBean.addUrlPatterns("/*");
            filterRegistrationBean.addInitParameter("profileEnable", "true");
            filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
            return filterRegistrationBean;
        }

        @Bean
        @Primary
        public DruidDataSource druidDataSource() {
            DruidDataSource datasource = new DruidDataSource();
            datasource.setUrl(datasourceProperties.getUrl());
            datasource.setUsername(datasourceProperties.getUsername());
            datasource.setPassword(datasourceProperties.getPassword());
            datasource.setDriverClassName(datasourceProperties.getDriverClassName());
            datasource.setInitialSize(datasourceProperties.getInitialSize());
            datasource.setMinIdle(datasourceProperties.getMinIdle());
            datasource.setMaxActive(datasourceProperties.getMaxActive());
            datasource.setMaxWait(datasourceProperties.getMaxWait());
            datasource.setTimeBetweenEvictionRunsMillis(datasourceProperties.getTimeBetweenEvictionRunsMillis());
            datasource.setMinEvictableIdleTimeMillis(datasourceProperties.getMinEvictableIdleTimeMillis());
            datasource.setValidationQuery(datasourceProperties.getValidationQuery());
            datasource.setTestWhileIdle(datasourceProperties.getTestWhileIdle());
            datasource.setTestOnBorrow(datasourceProperties.getTestOnBorrow());
            datasource.setTestOnReturn(datasourceProperties.getTestOnReturn());
            try {
                datasource.setFilters(datasourceProperties.getFilters());
            } catch (SQLException e) {
                logger.error("druid configuration initialization filter", e);
            }
            return datasource;
        }
    }